//const tagsCache = useLocalStorage('tags', [])
const settings = useLocalStorage('settings', {})

export const useMultiTagsStore = defineStore(
        'multiTags',
        {
            persist: settings.value.isTagsCache,
            state: () => ({
                multiTags: [],
            }),
            actions: {
                addTag(tag) {
                    if (this.multiTags.some(item => item.path === tag.path)) return
                    this.multiTags.push(
                            Object.assign({}, tag, {
                                title: tag.meta?.title ?? 'no-name'
                            })
                    )
                },
                async delTag(tag) {
                    let index = this.multiTags.findIndex(item => item.path === tag.path)
                    this.multiTags.splice(index, 1)
                },
                delTagByContext(tag, context) {
                    // 以下所有分支不会关闭粘性页签
                    let currentIndex = this.multiTags.findIndex(item => item.path === tag.path)
                    // 关闭其他页签
                    if (context === 'other') {
                        this.multiTags = this.multiTags.filter((tag, index) => index === currentIndex || tag?.meta?.affix)
                    }
                    // 关闭左边的页签
                    else if (context === 'left') {
                        this.multiTags = this.multiTags.filter((tag, index) => index >= currentIndex || tag?.meta?.affix)
                    }
                    // 关闭右边的页签
                    else if (context === 'right') {
                        this.multiTags = this.multiTags.filter((tag, index) => index <= currentIndex || tag?.meta?.affix)
                    }
                    // 关闭所有页签
                    else {
                        this.multiTags = this.multiTags.filter((tag, index) => tag?.meta?.affix)
                    }
                },
                clearCache(){
                    localStorage.removeItem("multiTags")
                }
            }
        }
)
